package com.jy.appmarket.cards;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.jy.app.market.idata.card.CardCategory;
import com.jy.app.market.idata.data.Category;
import com.jy.appmarket.R;
import com.jy.appmarket.activity.CategoryActivity;
import com.jy.appmarket.model.CategoryInfo;
import com.jy.appmarket.utils.LoadImageUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 类说明：
 * 
 * @author Baker.li
 * @date 2015年8月21日
 * @version 1.0
 */

public class CategoryUICard extends BaseUICard implements View.OnClickListener {
	private CardCategory mCategory;

	public CategoryUICard(Context context, CardCategory cardCategory) {
		super(context, R.layout.category_card_layout);
		mCategory = cardCategory;
	}

	@Override
	public void setupInnerViewElements(ViewGroup parent, View view) {
		if (mCategory == null) {
			return;
		}
		parent.findViewById(R.id.category_card_line2).setVisibility(View.VISIBLE);
		parent.findViewById(R.id.category_card_line3).setVisibility(View.VISIBLE);
		TextView title = parent.findViewById(R.id.cat_title);
		title.setText(mCategory.getName());
		parent.findViewById(R.id.cat_title_layout).setOnClickListener(this);
		ImageView icon = parent.findViewById(R.id.cat_title_icon);
		
		ImageLoader.getInstance().displayImage(mCategory.getIcon(), icon, LoadImageUtils.getCategroyIconDisplayOption(getContext()));

		List<Category> list = mCategory.getCategories();
		if (list == null || list.isEmpty()) {
			return;
		}

		int num;
		if (list.size() > 9) {
			num = 9;
		} else {
			num = list.size();
		}

		View[] subs = new View[] { parent.findViewById(R.id.cat_text_group_1),
				parent.findViewById(R.id.cat_text_group_2),
				parent.findViewById(R.id.cat_text_group_3),
				parent.findViewById(R.id.cat_text_group_4),
				parent.findViewById(R.id.cat_text_group_5),
				parent.findViewById(R.id.cat_text_group_6),
				parent.findViewById(R.id.cat_text_group_7),
				parent.findViewById(R.id.cat_text_group_8),
				parent.findViewById(R.id.cat_text_group_9) };
		LinearLayout subsGroup = parent.findViewById(R.id.category_card_subs);
		LinearLayout.LayoutParams lp = (LayoutParams) subsGroup.getLayoutParams();
		lp.weight = subsGroup.getChildCount();
		if (num <= 6) {
			lp.weight--;
			parent.findViewById(R.id.category_card_line3).setVisibility(View.GONE);
		}
		if (num <= 3) {
			lp.weight--;
			parent.findViewById(R.id.category_card_line2).setVisibility(View.GONE);
		}
		subsGroup.setLayoutParams(lp);

		for (View v : subs) {
			if (v != null) {
				v.setVisibility(View.INVISIBLE);
			}
		}
		for (int i = 0; i < num; i++) {
			subs[i].setVisibility(View.VISIBLE);
			TextView name = subs[i].findViewById(R.id.category_item_title);
			name.setText(list.get(i).getName());
			subs[i].setOnClickListener(this);
		}
	}

	@Override
	public int getType() {
		return 6;
	}

	@Override
	public void onClick(View v) {
		int index = -1;
		switch (v.getId()) {
		case R.id.cat_text_group_1:
			index = 0;
			break;
		case R.id.cat_text_group_2:
			index = 1;
			break;
		case R.id.cat_text_group_3:
			index = 2;
			break;
		case R.id.cat_text_group_4:
			index = 3;
			break;
		case R.id.cat_text_group_5:
			index = 4;
			break;
		case R.id.cat_text_group_6:
			index = 5;
			break;
		case R.id.cat_text_group_7:
			index = 6;
			break;
		case R.id.cat_text_group_8:
			index = 7;
			break;
		case R.id.cat_text_group_9:
			index = 8;
			break;
		case R.id.cat_title_layout:
			index = -1;
		default:
			break;
		}

		Intent it = new Intent(getContext(), CategoryActivity.class);
		it.putExtra(CategoryActivity.EXTRA_CATE_NAME, mCategory.getName());
		it.putExtra(CategoryActivity.EXTRA_CATE_PATH, mCategory.getUrl());
		if (mCategory.getCategories() != null) {
			ArrayList<CategoryInfo> subs = new ArrayList<CategoryInfo>();
			for (Category cat : mCategory.getCategories()) {
				CategoryInfo c = CategoryInfo.fromCategory(cat);
				subs.add(c);
			}
			it.putParcelableArrayListExtra(CategoryActivity.EXTRA_SUBS, subs);
		}
		it.putExtra(CategoryActivity.EXTRA_SUB_INDEX, index);
		getContext().startActivity(it);

	}

}